﻿Public Class Launcher

    Public Enum ExecVariant
        eExecVariant_RELEASE = 0
        eExecVariant_TRACE = 1
    End Enum

    Public oProjectInfo As ProjectInfo
    Public oVirtualMachine As VirtualMachine
    Public intVariant As ExecVariant
    Public strErrorMsg As String

    Public Event OnReceiveDataToTTY0(ByVal inData As String)
    Public Event OnOpenResults(ByVal inPath As String)

    Public Sub New(ByVal inVirtualMachine As VirtualMachine, ByVal inProjectInfo As ProjectInfo, ByVal inVariant As ExecVariant)
        intVariant = inVariant
        oProjectInfo = inProjectInfo
        oVirtualMachine = inVirtualMachine
    End Sub

    Private Function CopyExecToVM() As Boolean
        ' Check if executable exists
        Dim strExecFile As String = ""

        If intVariant = ExecVariant.eExecVariant_TRACE Then
            strExecFile = PutSlash(oProjectInfo.GetExeTraceDir) & oProjectInfo.strName
        Else
            strExecFile = PutSlash(oProjectInfo.GetExeReleaseDir) & oProjectInfo.strName
        End If
        If Not IO.File.Exists(strExecFile) Then
            strErrorMsg = "Executable file not found."
            Return False
        End If

        ' Copy exec in VM using FTP
        Dim oFTPclient As FTPclient
        oFTPclient = New FTPclient(oVirtualMachine.GetIPForFTP, oVirtualMachine.strUsername, oVirtualMachine.strPassword)
        If Not oFTPclient.Upload(strExecFile) Then
            strErrorMsg = "Impossible upload executable file to target."
            Return False
        End If
        oFTPclient = Nothing

        Return True
    End Function

    Private Function ExecViaTelnet(ByVal inNameResultsFile As String, ByVal inNameListingFile As String) As Boolean
        Dim oTelnetClientTTY0 As New TelnetClient(oVirtualMachine.strIP, 23)
        
        AddHandler oTelnetClientTTY0.OnReceivData, AddressOf OnReceiveDataToTTY0Local
        
        ' Login TTY0
        If Not oTelnetClientTTY0.Login(oVirtualMachine.strUsername, oVirtualMachine.strPassword, 1500) Then
            strErrorMsg = "Problem login via telnet TTY0." & vbCrLf & oTelnetClientTTY0.strErrorMsg
            Return False
        End If

        ' Show which tty is
        If Not oTelnetClientTTY0.WriteAndWait("tty", "# ", 200, True) Then
            strErrorMsg = "Problem executing 'tty' via telnet." & vbCrLf & oTelnetClientTTY0.strErrorMsg
            Return False
        End If

        ' Execute in TTY0

        ' Change dir to TMP/UT
        If Not oTelnetClientTTY0.WriteAndWait("cd ../tmp/UT", "# ", 200, False) Then
            strErrorMsg = "Problem executing 'cd' via telnet." & vbCrLf & oTelnetClientTTY0.strErrorMsg
            Return False
        End If

        ' Delete xml files
        If Not oTelnetClientTTY0.WriteAndWait("rm -f " & inNameListingFile, "# ", 200, False) Then
            strErrorMsg = "Problem executing 'rm' via telnet." & vbCrLf & oTelnetClientTTY0.strErrorMsg
            Return False
        End If
        If Not oTelnetClientTTY0.WriteAndWait("rm -f " & inNameResultsFile, "# ", 200, False) Then
            strErrorMsg = "Problem executing 'rm' via telnet." & vbCrLf & oTelnetClientTTY0.strErrorMsg
            Return False
        End If

        ' Change permission to execute program
        If Not oTelnetClientTTY0.WriteAndWait(String.Format("chmod 766 {0}", oProjectInfo.strName), "# ", 200, False) Then
            strErrorMsg = "Problem executing 'chmod' via telnet." & vbCrLf & oTelnetClientTTY0.strErrorMsg
            Return False
        End If

        ' Execute program
        If Not oTelnetClientTTY0.WriteAndWait(String.Format("./{0}", oProjectInfo.strName), "# ", 200, True) Then
            strErrorMsg = "Problem executing program via telnet." & vbCrLf & oTelnetClientTTY0.strErrorMsg
            Return False
        End If

        ' Delete program
        If Not oTelnetClientTTY0.WriteAndWait(String.Format("rm -f {0}", oProjectInfo.strName), "# ", 200, False) Then
            strErrorMsg = "Problem executing 'rm' via telnet." & vbCrLf & oTelnetClientTTY0.strErrorMsg
            Return False
        End If

        ' Close telnet sessin TTY0
        If Not oTelnetClientTTY0.EndSession() Then
            strErrorMsg = "Problem closing telnet session TTY0." & vbCrLf & oTelnetClientTTY0.strErrorMsg
            Return False
        End If

        oTelnetClientTTY0 = Nothing
        
        Return True
    End Function

    Public Function LaunchUT() As Boolean
        Try
            Dim strNameResultsFile As String = String.Format("{0}-Results.xml", oProjectInfo.strName)
            Dim strNameListingFile As String = String.Format("{0}-Listing.xml", oProjectInfo.strName)

            ' Copy executable to VM
            If CopyExecToVM() = False Then
                Return False
            End If

            ' Exec via Telnet
            If ExecViaTelnet(strNameResultsFile, strNameListingFile) = False Then
                Return False
            End If
            
            ' Get xml files from VM using FTP
            Dim oFTPclient As FTPclient
            oFTPclient = New FTPclient(oVirtualMachine.GetIPForFTP, oVirtualMachine.strUsername, oVirtualMachine.strPassword)
            If Not oFTPclient.Download(strNameResultsFile, PutSlash(oProjectInfo.GetResDir) & strNameResultsFile, True) Then
                strErrorMsg = "Impossible download xml files from target."
                Return False
            End If
            If Not oFTPclient.Download(strNameListingFile, PutSlash(oProjectInfo.GetResDir) & strNameListingFile, True) Then
                strErrorMsg = "Impossible download xml files from target."
                Return False
            End If
            oFTPclient = Nothing

            ' Open xml files
            RaiseEvent OnOpenResults(PutSlash(oProjectInfo.GetResDir) & strNameResultsFile)

            Return True
        Catch ex As Exception
            strErrorMsg = ex.Message
            Return True
        End Try
    End Function

    Private Sub OnReceiveDataToTTY0Local(ByVal inData As String)
        RaiseEvent OnReceiveDataToTTY0(inData)
    End Sub

End Class
